using System;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using eWorld.UI;
using TimeSheetBLL;

namespace TimeSheet.Protected.View
{
    /// <summary>
    /// A nice description of EntityControl goes here.
    /// 
    /// Version: $Id: EntityControl.cs,v 1.1 2004/03/08 23:56:37 jperrin Exp $
    /// 
    /// Date: 
    /// Copyright: 2004, Kanga Communications, Calgary, Alberta, Canada. 
    /// </summary>
    public class EntityControl : BaseControl
    {
        private IDBEntity boundEntity;

        public virtual IDBEntity BoundEntity
        {
            get { return boundEntity; }
            set { boundEntity = value; }
        }

        public virtual void BindControlToEntity()
        {
            if (boundEntity != null)
            {
                LoadControlsFromIDBEntity(boundEntity);
            }
        }

        public virtual void LoadEntityFromForm()
        {
            if (boundEntity != null)
            {
                LoadIDBEntityFromControls(boundEntity);
            }
        }

        protected virtual void LoadIDBEntityFromControls(IDBEntity entity)
        {
            PropertyInfo[] piArray = entity.GetType().GetProperties();

            foreach (PropertyInfo prop in piArray)
            {
                Object[] param = new Object[1];
                if (prop.Name != "IsValid" &&
                    prop.Name != "ErrorMessage")
                {
                    Type propType = prop.GetGetMethod().ReturnType;
                    Control controlToBind = FindControl(prop.Name);

                    if (controlToBind != null)
                    {
                        if (controlToBind is TextBox)
                        {
                            TextBox tb = (TextBox) controlToBind;
                            if (tb.Text.Length > 0)
                            {
                                param[0] = tb.Text;
                            }
                        }
                        else if (controlToBind is DropDownList)
                        {
                            DropDownList ddl = (DropDownList) controlToBind;
                            int selectedIndex = ddl.SelectedIndex;

                            if (prop.PropertyType.ToString() == "System.Int32")
                            {
                                param[0] = Convert.ToInt32(ddl.SelectedItem.Value);
                            }
                            else if (prop.PropertyType.ToString() == "System.Int64")
                            {
                                param[0] = Convert.ToInt64(ddl.SelectedItem.Value);
                            }
                            else if (prop.PropertyType.ToString() == "System.String")
                            {
                                param[0] = ddl.SelectedItem.Value;
                            }
                            else
                            {
                                throw new Exception(
                                    prop.PropertyType + " Is not a handled type");
                            }
                        }
                        else if (controlToBind is CalendarPopup)
                        {
                            CalendarPopup cal = (CalendarPopup) controlToBind;
                            param[0] = cal.SelectedDate;
                        }
                        else if (controlToBind is CheckBox)
                        {
                            CheckBox cb = (CheckBox) controlToBind;
                            param[0] = cb.Checked;
                        }
                        else if (controlToBind is NumericBox)
                        {
                            NumericBox nb = (NumericBox) controlToBind;
                            param[0] = nb.Text;
                        }
                        else
                        {
                            throw new Exception("Can't be bound to this control");
                        }
                        prop.SetValue(entity, param[0], null);
                    }
                }
            }
        }

        protected virtual void LoadControlsFromIDBEntity(IDBEntity entity)
        {
            foreach (Control control in Controls)
            {
                LoadControl(control, entity);
            }
        }

        private void LoadControl(Control control, IDBEntity entity)
        {
            //Trace.Write( "-JEFF", "Found a control" );
            PropertyInfo prop = null;
            if (control.ID != null)
            {
                prop = entity.GetType().GetProperty(control.ID);
                //Trace.Write( "--JEFF", control.ID.ToString() );
            }
            if (control is CollapsablePanel)
            {
                //Trace.Write( "----JEFF", "Found a collapseable panel" );
                foreach (Control cont in control.Controls)
                {
                    LoadControl(cont, entity);
                }
                return;
            }
            if (prop != null)
            {
                //Trace.Write( "---JEFF", prop.GetValue( entity, null ).ToString() );
                Object propValue = prop.GetValue(entity, null);

                if (control is TextBox)
                {
                    TextBox tb = (TextBox) control;
                    tb.Text = Convert.ToString(propValue);
                }
                else if (control is DropDownList)
                {
                    DropDownList ddl = (DropDownList) control;
                    if (propValue != null)
                    {
                        ddl.SelectedIndex =
                            ddl.Items.IndexOf(ddl.Items.FindByValue(propValue.ToString()));
                    }
                }
                else if (control is CalendarPopup)
                {
                    CalendarPopup cal = (CalendarPopup) control;
                    cal.SelectedDate = Convert.ToDateTime(propValue);
                }
                else if (control is CheckBox)
                {
                    CheckBox cb = (CheckBox) control;
                    cb.Checked = Convert.ToBoolean(propValue);
                }
                else if (control is NumericBox)
                {
                    NumericBox nb = (NumericBox) control;
                    nb.Text = propValue.ToString();
                }
                else if (control is Label)
                {
                    Label label = (Label) control;
                    label.Text = propValue.ToString();
                }
                else
                {
                }
            }
        }

        protected virtual void ClearForm()
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox)
                {
                    TextBox tb = (TextBox) c;
                    tb.Text = "";
                }
                if (c is CalendarPopup)
                {
                    CalendarPopup cp = (CalendarPopup) c;
                    cp.Text = "";
                }
                if (c is CheckBox)
                {
                    CheckBox cb = (CheckBox) c;
                    cb.Checked = false;
                }
                if (c is DropDownList)
                {
                    DropDownList ddl = (DropDownList) c;
                    ddl.SelectedIndex = 0;
                }
            }
        }

        protected virtual void DisableAllControlsOnForm()
        {
            foreach (Control c in Controls)
            {
                if (c is WebControl)
                {
                    WebControl wc = (WebControl) c;
                    wc.Enabled = false;
                }
            }
        }

        protected virtual long SaveBoundEntity()
        {
            Page.Validate();
            if (Page.IsValid)
            {
                return BrokerFactory.Save(boundEntity);
            }
            return -1;
        }
    }
}